package com.bootdo.analyze.controller;

import com.bootdo.analyze.domain.GoodsSellAIDO;
import com.bootdo.analyze.service.GoodsSellAIService;
import com.bootdo.analyze.util.*;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品AI分析
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-03-02 10:02:27
 */
 
@Controller
@RequestMapping("/analyze/goods")
public class GoodsSellAIController {
	@Autowired
	private GoodsSellAIService goodsSellAIService;

	/**
	 * 畅销商品分析
	 * @return
	 */
	@GetMapping("/fast")
	@RequiresPermissions("analyze:fastmovingGoods:fastgoods")
	String fastGoods(){
	    return "analyze/fastmovingGoods/fastgoods";
	}
	
	@ResponseBody
	@GetMapping("/fastgoodslist")
	@RequiresPermissions("analyze:fastmovingGoods:fastgoods")
	public PageUtils fastGoodsList(@RequestParam Map<String, Object> params){
		//查询列表数据

        Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.list(query);
		List<GoodsSellAIDO> goodsListNew = new ArrayList<GoodsSellAIDO>();
		String saleSrules = params.get("saleSrules").toString();
		int total = goodsSellAIService.count(query);
		if (saleSrules!=null && saleSrules!=""){
			Double doSaleSrules = Double.parseDouble(saleSrules);
			for (GoodsSellAIDO goodsSellAIDO:goodsList){
				if (goodsSellAIDO.getSalespercentage()>=doSaleSrules){
					goodsListNew.add(goodsSellAIDO);
				}
			}
		}
		total=goodsListNew.size();

		PageUtils pageUtils = new PageUtils(goodsListNew, total);
		return pageUtils;
	}


	@GetMapping("/fastgoodsreport")
	String fastgoodsReportShows(){
		return "analyze/fastmovingGoods/fastgoodsreport";
	}

	@GetMapping("/fastgoodsrules")
	String fastgoodsRules(){
		return "analyze/fastmovingGoods/fastgoodsrules";
	}

	/**
	 * 滞销商品分析
	 * @return
	 */
	@GetMapping("/slow")
	@RequiresPermissions("analyze:slowmovingGoods:slowgoods")
	String slowGoods(){
		return "analyze/slowmovingGoods/slowgoods";
	}

	@ResponseBody
	@GetMapping("/slowgoodslist")
	@RequiresPermissions("analyze:slowmovingGoods:slowgoods")
	public PageUtils slowGoodsList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.list(query);
		List<GoodsSellAIDO> goodsListNew = new ArrayList<GoodsSellAIDO>();
		String saleSrules = params.get("saleSrules").toString();
		int total = goodsSellAIService.count(query);
		if (saleSrules!=null && saleSrules!=""){
			Double doSaleSrules = Double.parseDouble(saleSrules);
			for (GoodsSellAIDO goodsSellAIDO:goodsList){
				if (goodsSellAIDO.getSalespercentage()<=doSaleSrules){
					goodsListNew.add(goodsSellAIDO);
				}
			}
		}
		total=goodsListNew.size();

		PageUtils pageUtils = new PageUtils(goodsListNew, total);
		return pageUtils;
	}


	@GetMapping("/slowgoodsreport")
	String slowgoodsReportShows(){
		return "analyze/slowmovingGoods/slowgoodsreport";
	}

	@GetMapping("/slowgoodsrules")
	String slowgoodsRules(){
		return "analyze/slowmovingGoods/slowgoodsrules";
	}

	/**
	 * 商品销售波动
	 * @return
	 */
	@GetMapping("/goodsfluctuate")
	@RequiresPermissions("analyze:sellgoodsFluctuate:goodsfluctuate")
	String goodsFluctuate(){
		return "analyze/sellgoodsFluctuate/goodsfluctuate";
	}

	@ResponseBody
	@GetMapping("/goodsfluctuatelist")
	@RequiresPermissions("analyze:sellgoodsFluctuate:goodsfluctuate")
	public PageUtils goodsList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.getGoodsFluctuate(query);
		System.out.println("----------------------"+goodsList.size());
		//List<Dto> goodsList = goodsSellAIService.getGoodsFluctuate1(query);
		int total = goodsSellAIService.count(query);
		PageUtils pageUtils = new PageUtils(goodsList, total);
		return pageUtils;
	}

	@GetMapping("/goodsfluctuatereport")
	String sellgoodsReportShows(){
		return "analyze/sellgoodsFluctuate/fluctuatereport";
	}

	@GetMapping("/fluctuatecharthtml/{id}")
	String post(@PathVariable("id") Integer id, Model model) {
		model.addAttribute("id",id);

		return "analyze/sellgoodsFluctuate/fluctuatechart";
	}


	@ResponseBody
	@RequestMapping("/goodsfluctuatechart")
	public ChartsUtil goodsListChart(@RequestParam String id){
		ChartsUtil chartsUtil = new ChartsUtil();
		Title title = new Title();
		title.setText("销售波动图");
		chartsUtil.setTitle(title);
		chartsUtil.setTooltip(new Tooltip());
		Legend legend = new Legend();
		List<String> list = new ArrayList<String>();
		list.add("销售数量");
		list.add("销售金额");
		legend.setData(list);
		List<WeiDuUtil> weiDuUtilList = goodsSellAIService.getGoodsInfo(Integer.parseInt(id));
		List<Integer> countWDList = new ArrayList<Integer>();
		List<Double> pricesWDList = new ArrayList<Double>();
		List<Date>  businessDayList = new ArrayList<Date>();
		for (WeiDuUtil weiDuUtil:weiDuUtilList){
			countWDList.add(weiDuUtil.getCountWD());
			pricesWDList.add(weiDuUtil.getPricesWD());
			businessDayList.add(weiDuUtil.getBusinessDay());
		}
		XAxis xAxis = new XAxis();
		xAxis.setData(businessDayList);
		chartsUtil.setxAxis(xAxis);
		YAxis yAxis = new YAxis();
		chartsUtil.setyAxis(yAxis);
		Series series = new Series();
		series.setName("销售数量");
		series.setType("bar");
		series.setData(countWDList);


		chartsUtil.setLegend(legend);


		return chartsUtil;
	}



	/**
	 * 进货商品分析
	 * @return
	 */
	@GetMapping("/stockfluctuate")
	@RequiresPermissions("analyze:stockgoodsFluctuate:stockfluctuate")
	String stockFluctuate(){
		return "analyze/stockgoodsFluctuate/stockfluctuate";
	}

	@ResponseBody
	@GetMapping("/stockfluctuatelist")
	@RequiresPermissions("analyze:stockgoodsFluctuate:stockfluctuate")
	public PageUtils stockList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.getStockFluctuate(query);
		int total = goodsSellAIService.count(query);
		PageUtils pageUtils = new PageUtils(goodsList, total);
		return pageUtils;
	}

	@GetMapping("/stockfluctuatereport")
	String stockReportShows(){
		return "analyze/stockgoodsFluctuate/fluctuatereport";
	}


	/**
	 * 库存周转分析
	 * @return
	 */
	@GetMapping("/inventoryturnover")
	@RequiresPermissions("analyze:inventoryturnoverAI:inventoryturnover")
	String inventoryTurnover(){
		return "analyze/inventoryturnoverAI/inventoryturnover";
	}

	@ResponseBody
	@GetMapping("/inventoryturnoverlist")
	@RequiresPermissions("analyze:inventoryturnoverAI:inventoryturnover")
	public PageUtils inventoryTurnoverList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.getInventoryTurnover(query);
		//List<Dto> goodsList = goodsSellAIService.getGoodsFluctuate1(query);
		int total = goodsSellAIService.count(query);
		PageUtils pageUtils = new PageUtils(goodsList, total);
		return pageUtils;
	}





	@GetMapping("/inventoryturnoverreport")
	String inventoryTurnoverReport(){
		return "analyze/inventoryturnoverAI/inventoryturnoverreport";
	}

	@GetMapping("/inventoryturnoverrules")
	String inventoryturnoverRules(){
		return "analyze/inventoryturnoverAI/inventoryturnoverrules";
	}



	/**
	 * 库存积压分析
	 * @return
	 */
	@GetMapping("/inventorybacklog")
	@RequiresPermissions("analyze:inventorybacklogAI:inventorybacklog")
	String inventoryBacklog(){
		return "analyze/inventorybacklogAI/inventorybacklog";
	}

	@ResponseBody
	@GetMapping("/inventorybackloglist")
	@RequiresPermissions("analyze:inventorybacklogAI:inventorybacklog")
	public PageUtils inventoryBacklogList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsSellAIDO> goodsList = goodsSellAIService.getInventoryBacklog(query);
		//List<Dto> goodsList = goodsSellAIService.getGoodsFluctuate1(query);
		int total = goodsSellAIService.count(query);
		PageUtils pageUtils = new PageUtils(goodsList, total);
		return pageUtils;
	}

	@GetMapping("/inventorybacklogreport")
	String inventoryBacklogReport(){
		return "analyze/inventorybacklogAI/inventorybacklogreport";
	}

	@GetMapping("/inventorybacklogrules")
	String inventoryBacklogRules(){
		return "analyze/inventorybacklogAI/inventorybacklogrules";
	}





}
